package pl.mcnova.view.panels;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.TableRowSorter;

import pl.mcnova.model.dao.ContractorDAO;
import pl.mcnova.model.entity.Contractor;
import pl.mcnova.model.table.ContractorTableModel;
import pl.mcnova.view.filters.ContractorFilter;

public class MainPanel extends JPanel {
	private JScrollPane scrollPane;
	private JTable table;
	private ContractorTableModel model;
	private String type;
	private ContractorDAO dao;
	private TableRowSorter<ContractorTableModel> sorter;

	public MainPanel(String name) {
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(name),
				BorderFactory.createEmptyBorder(0, 6, 6, 6)));

		type = name;
		dao = new ContractorDAO();

		model = new ContractorTableModel();

		table = new JTable(model);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		table.getTableHeader().setReorderingAllowed(false);

		scrollPane = new JScrollPane(table);

		sorter = new TableRowSorter<ContractorTableModel>(model);
		table.setRowSorter(sorter);

		add(scrollPane, BorderLayout.CENTER);

		// filterPanel = new FilterPanel();
		// add(filterPanel, BorderLayout.NORTH);
	}

	public int getSelected() {
		int[] selected = table.getSelectedRows();

		if (selected == null || selected.length == 0)
			return -1;
		else {
			if (sorter != null) return sorter.convertRowIndexToModel(selected[0]);
			return selected[0];
		}
	}

	public int[] getAllSelected() {
		int[] selected = table.getSelectedRows();

		if (selected == null || selected.length == 0)
			return null;
		else {
			if (sorter != null) for (int i = 0; i < selected.length; i++)
				selected[i] = sorter.convertRowIndexToModel(selected[i]);

			return selected;
		}

	}

	public List<Contractor> getAllSelectedContractors() {
		int[] selected = getAllSelected();
		if (selected == null) return null;

		List<Contractor> list = new ArrayList<Contractor>();

		for (int i = 0; i < selected.length; i++) {
			list.add(model.get(selected[i]));
		}

		return list;

	}

	public Contractor getSelectedContractor() {
		int i = getSelected();
		if (i < 0) return null;
		return model.get(i);
	}

	public Integer getSelectedContractorId() {
		Contractor c = getSelectedContractor();
		if (c != null) return c.getId();
		return -1;
	}

	public ListSelectionModel getTable() {
		return table.getSelectionModel();

	}

	public void clearSelection() {
		table.getSelectionModel().removeSelectionInterval(table.getSelectionModel().getMinSelectionIndex(),
				table.getSelectionModel().getMaxSelectionIndex());
	}

	public void setModel(List<Contractor> list) {
		model.setData(list);
	}

	public String getType() {
		return type;
	}

	public void addRow(Integer id) {
		model.add(dao.get(id));
	}

	public void updateRow(Integer id) {
		for (int i = 0; i < model.getRowCount(); i++) {
			if (model.get(i).getId() == id) {
				model.update(dao.get(id), i);
				break;
			}
		}
	}

	public void remove(Integer id) {
		model.remove(model.getIndex(id));
	}

	public String getType(int row) {
		Contractor c = model.get(row);
		if (c != null) return c.getContractorType();
		return "";
	}

	public Contractor getContractor(int row) {
		return model.get(row);
	}

	public boolean isSelectedEmail() {
		Contractor c = getSelectedContractor();
		if (c == null || c.getEmail() == null || "".equals(c.getEmail())) return false;
		return true;
	}

	public void runFilter(Contractor c, Integer deviceNumber, String phoneNumber) {

		List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>();

		if (c == null) {
			filters.add(RowFilter.regexFilter("", 1));
			filters.add(RowFilter.regexFilter("", 2));
			filters.add(RowFilter.regexFilter("", 3));
			filters.add(RowFilter.regexFilter("", 4));
			filters.add(RowFilter.regexFilter("", 5));
			filters.add(RowFilter.regexFilter("", 6));
			filters.add(RowFilter.regexFilter("", 7));
			filters.add(RowFilter.regexFilter("", 8));
		} else {
			filters.add(RowFilter.regexFilter("(?i)" + c.getName(), 1));
			filters.add(RowFilter.regexFilter("(?i)" + c.getStreet(), 2));
			filters.add(RowFilter.regexFilter("(?i)" + c.getHouseNumber(), 3));
			filters.add(RowFilter.regexFilter("(?i)" + c.getPostalCode(), 4));
			filters.add(RowFilter.regexFilter("(?i)" + c.getCity(), 5));
			filters.add(RowFilter.regexFilter("(?i)" + c.getNip(), 6));
			filters.add(RowFilter.regexFilter("(?i)" + c.getEmail(), 7));
			filters.add(RowFilter.regexFilter("(?i)" + c.getWww(), 8));
		}

		filters.add(new ContractorFilter(phoneNumber, deviceNumber, c.getStatus()));

		RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
		sorter.setRowFilter(rf);
	}

	public void runFilter() {

		List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>();

		filters.add(RowFilter.regexFilter("", 1));
		filters.add(RowFilter.regexFilter("", 2));
		filters.add(RowFilter.regexFilter("", 3));
		filters.add(RowFilter.regexFilter("", 4));
		filters.add(RowFilter.regexFilter("", 5));
		filters.add(RowFilter.regexFilter("", 6));
		filters.add(RowFilter.regexFilter("", 7));
		filters.add(RowFilter.regexFilter("", 8));

		RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
		sorter.setRowFilter(rf);
	}
}
